import { defineStore } from 'pinia'
import { ref } from 'vue'
import { useGetGoodsCategoryService } from '@/api/requsets/index.js'

export const useGoodsStore = defineStore(
  'useGoodsStore',
  () => {
    // 轮播图 列表
    const imgList = ref([])
    // 导航列表
    const navigations = ref([])
    // 商品列表
    const goodsList = ref([])

    // 分类列表
    const categoryList = ref([])

    // 初始化数据方法
    const saveData = (
      newImgList = [],
      newNavigations = [],
      newGoodsList = []
    ) => {
      imgList.value = newImgList
      navigations.value = newNavigations
      goodsList.value = newGoodsList
    }

    // 初始化分类列表
    const saveCategoryList = async () => {
      const {
        data: { list }
      } = await useGetGoodsCategoryService()
      categoryList.value = list
      // console.log(categoryList.value)
    }

    // 获取商品详情
    // const getGoodsDetail = (id) => {
    //   return goodsList.value.find((item) => item.id === id)
    // }

    // 获取分类对应的商品
    const getCategoryChildren = (id) => {
      if (!id) return categoryList.value[0].children
      console.log('id', id)
      return categoryList.value.find((item) => item.category_id === id).children
    }

    return {
      imgList,
      navigations,
      goodsList,
      saveData,
      // getGoodsDetail,
      categoryList,
      saveCategoryList,
      getCategoryChildren
    }
  },
  {
    persist: true // 持久化
  }
)
